package cn.livingCloud.controller.core.validator;

import cn.livingCloud.dto.CasePendingFormDto;
import cn.livingCloud.entity.core.CaseStatus;
import cn.livingCloud.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * Created by 唐旭 on 2016/6/21.
 */
@Component
public class CasePendingFormDtoValidator implements Validator {
    @Autowired
    private CaseService caseService;

    @Override
    public boolean supports(Class<?> aClass) {
        return CasePendingFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        CasePendingFormDto formDto = (CasePendingFormDto) target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "caseUuid", null, "参数错误");
        if (!formDto.isAllow()) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "content", null, "驳回原因不能为空");
        }
        CaseStatus status = caseService.getCaseStatus(formDto.getCaseUuid());
        if (!status.isPending()) {
            errors.rejectValue("content", null, "案件状态必须为待审核状态");
        }
    }
}
